package com.fqgj.xjd.user.service.impl;
/**
 * Created by zhangxingyun on 17/7/23.
 */

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.xjd.user.client.UserAuthInfoService;
import com.fqgj.xjd.user.client.enums.AuthStatusEnum;
import com.fqgj.xjd.user.client.response.*;
import com.fqgj.xjd.user.dao.*;
import com.fqgj.xjd.user.entity.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:
 * @Description: 认证信息获取服务
 * @Author zhangxingyun
 * @Create 2017-07-23 15:16
 */
@Service("userAuthInfoService")
public class UserAuthInfoServiceImpl implements UserAuthInfoService {

    @Autowired
    UserAuthXuexinDao userAuthXuexinDao;

    @Autowired
    UserAuthDeliveryAddressDao userAuthDeliveryAddressDao;

    @Autowired
    UserAuthTaobaoDao userAuthTaobaoDao;

    @Autowired
    UserAuthTaxDao userAuthTaxDao;

    @Autowired
    UserAuthEmailDao userAuthEmailDao;

    /**
     * 学信网信息获取
     *
     * @param userCode
     * @return
     */
    @Override
    public Response<UserXuexinResponse> getUserXueXinByUserCode(String userCode) {
        UserAuthXuexinEntity userAuthXuexinEntity = userAuthXuexinDao.selectXueXinByUserCode(userCode);
        UserXuexinResponse xuexin = new UserXuexinResponse();
        if (userAuthXuexinEntity == null) {
            return Response.ok();
        }
        BeanUtils.copyProperties(userAuthXuexinEntity, xuexin);
        return Response.ok().putData(xuexin);
    }

    /**
     * 电商收货地址信息获取
     *
     * @param userCode
     * @return
     */
    @Override
    public Response<List<UserDeliveryAddress>> getUserDeliveryAddressList(String userCode) {

        List<UserAuthDeliveryAddressEntity> userAuthDeliveryAddressEntityList = userAuthDeliveryAddressDao.selectUserAuthDeliveryAddressListByUserCode(userCode);


        List<UserDeliveryAddress> userDeliveryAddressList = new ArrayList<>();
        for (UserAuthDeliveryAddressEntity userAuthDeliveryAddressEntity : userAuthDeliveryAddressEntityList) {
            UserDeliveryAddress userDeliveryAddress = new UserDeliveryAddress();
            BeanUtils.copyProperties(userAuthDeliveryAddressEntity, userDeliveryAddress);
            userDeliveryAddressList.add(userDeliveryAddress);
        }

        return Response.ok().putData(userDeliveryAddressList);
    }

    /**
     * 添加淘宝信息
     *
     * @param userCode
     * @param taobaoUrl
     * @return
     */
    @Override
    public Response<Boolean> addUserTaobao(String userCode, String taobaoUrl) {
        if (StringUtils.isEmpty(taobaoUrl)) {
            return Response.ok(false);
        }
        UserAuthTaobaoEntity userAuthTaobaoEntity = new UserAuthTaobaoEntity();
        userAuthTaobaoEntity.setUserCode(userCode);
        userAuthTaobaoEntity.setStatus(AuthStatusEnum.AUTH_SUCCESS.getStatus());
        userAuthTaobaoEntity.setTaobaoUrl(taobaoUrl);
        userAuthTaobaoDao.insert(userAuthTaobaoEntity);
        return Response.ok();
    }

    /**
     * 添加个税信息
     *
     * @param userCode
     * @param taxUrl
     * @return
     */
    @Override
    public Response<Boolean> addUserTax(String userCode, String taxUrl) {
        if (StringUtils.isEmpty(taxUrl)) {
            return Response.ok(false);
        }
        UserAuthTaxEntity userAuthTaxEntity = new UserAuthTaxEntity();
        userAuthTaxEntity.setUserCode(userCode);
        userAuthTaxEntity.setStatus(AuthStatusEnum.AUTH_SUCCESS.getStatus());
        userAuthTaxEntity.setTaxUrl(taxUrl);
        userAuthTaxDao.insert(userAuthTaxEntity);
        return Response.ok();
    }

    /**
     * @param userCode
     * @return
     */
    @Override
    public Response<UserAuthTaobao> getUserTaobao(String userCode) {

        UserAuthTaobao userAuthTaobao = new UserAuthTaobao();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userCode", userCode);
        List<UserAuthTaobaoEntity> list = userAuthTaobaoDao.selectByParams(paramMap);
        if (CollectionUtils.isEmpty(list)) {
            return Response.ok().putData(userAuthTaobao);
        }
        BeanUtils.copyProperties(list.get(0), userAuthTaobao);
        return Response.ok().putData(userAuthTaobao);
    }

    @Override
    public Response<UserAuthTax> getUserTax(String userCode) {
        UserAuthTax userAuthTax = new UserAuthTax();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userCode", userCode);
        List<UserAuthTaxEntity> list = userAuthTaxDao.selectByParams(paramMap);
        if (CollectionUtils.isEmpty(list)) {
            return Response.ok().putData(userAuthTax);
        }
        BeanUtils.copyProperties(list.get(0), userAuthTax);
        return Response.ok().putData(userAuthTax);
    }


}
